package com.briup.cms.service;

import com.briup.cms.bean.Carousel;
import com.briup.cms.dao.CarouselMapper;
import com.briup.cms.exception.CustomerException;
import com.briup.cms.utils.MessageStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CarouselServiceImpl implements ICarouselService{

    @Autowired
    private CarouselMapper mapper;

    @Override
    public PageInfo<Carousel> findAll(int pageNum, int pageSize) throws CustomerException {
        PageHelper.startPage(pageNum, pageSize,true);
        List<Carousel> list = mapper.findAll(pageNum,pageSize);
        PageInfo<Carousel> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public void deleteById(int id) throws CustomerException {

        if(mapper.findById(id) == null){
            throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_CAROUSEL_NOT_EXIST);
        }
        mapper.deleteById(id);
        System.out.println(".....正在删除轮播图id为"+id);
    }


    @Override
    public void saveCarousel(Carousel carousel) throws CustomerException {
        // 如果已存在轮播图有相同的url，则抛出异常
        if(mapper.findByUrl(carousel.getCarouselUrl())!=null){
            throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_CAROUSEL_URL_EXIST);
        }
        mapper.saveCarousel(carousel);
        System.out.println("正在保存轮播图信息："+carousel);

    }

    @Override
    public void updateCarouselByStatus(int id, int status) throws CustomerException {
        mapper.updateCarouselByStatus(id,status);
        System.out.println(".....正在更新轮播图为"+id+"的状态为"+status);
    }

    @Override
    public void updateCarousel(Carousel carousel) throws CustomerException {
        mapper.updateCarousel(carousel);
    }


    @Override
    public List<Carousel> findYesCarousel() throws CustomerException {
        return mapper.findYesCarousel();
    }
}
